package com.zhengqing.softdemo.service.impl

import com.mybatisflex.core.paginate.Page
import com.mybatisflex.core.query.If
import com.mybatisflex.core.query.QueryWrapper
import com.mybatisflex.core.util.StringUtil
import com.zhengqing.softdemo.entity.Soft
import com.zhengqing.softdemo.mapper.SoftMapper
import com.zhengqing.softdemo.service.SoftService
import com.mybatisflex.spring.service.impl.ServiceImpl
import org.springframework.stereotype.Service

import com.zhengqing.softdemo.entity.table.SoftTableDef

import com.zhengqing.softdemo.config.Slf4j;
import com.zhengqing.softdemo.config.Slf4j.Companion.log;


/**
 * 服务层实现。
 *
 * @author zql
 * @since 2024-04-25
 */
@Slf4j
@Service
class SoftServiceImpl : ServiceImpl<SoftMapper?, Soft?>(), SoftService {
    override fun queryFuzzy(current: Int?, size: Int?, softId: String?, softName: String?, softVersion: String?, gitUrl: String?, officialUrl: String?, mainLanguage: String?, languages: String?, startTime: String?, endTime: String?): Page<Soft?>? {
        val queryWrapper = QueryWrapper()
        queryWrapper.select(*SoftTableDef.SOFT.DEFAULT_COLUMNS).from(SoftTableDef.SOFT)
                .where(SoftTableDef.SOFT.SOFT_NAME.like(softName) { string: String? -> If.hasText(string) })
                .and(SoftTableDef.SOFT.SOFT_VERSION.like(softVersion) { string: String? -> If.hasText(string) })
                .and(SoftTableDef.SOFT.GIT_URL.like(gitUrl) { string: String? -> If.hasText(string) })
                .and(SoftTableDef.SOFT.OFFICIAL_URL.like(officialUrl) { string: String? -> If.hasText(string) })
                .and(SoftTableDef.SOFT.MAIN_LANGUAGE.like(mainLanguage) { str: String? -> StringUtil.isNotBlank(str) })
                .and(SoftTableDef.SOFT.LANGUAGES.like(languages) { string: String? -> If.hasText(string) })
                .and(SoftTableDef.SOFT.SOFT_ID.like(softId) { string: String? -> If.hasText(string) })
                .and(SoftTableDef.SOFT.UPDATED_AT.between("$startTime 00:00:00", "$endTime 23:59:59").`when`("" != startTime && "" != endTime))
                .orderBy(SoftTableDef.SOFT.UPDATED_AT, false) // asc=false 就是desc!
        //.orderByDesc(SOFT.UPDATED_AT); // mybatis-flex 时间类型不支持orderByDesc??
        log.info("queryWrapper::: " + queryWrapper.toSQL())
        val record: Page<Soft?> = page(Page(
                current,
                size
        ), queryWrapper)
        log.info("record::: $record")
        return record
    }

    override fun pageDef(current: Int?, size: Int?, softName: String?): Page<Soft?>? {
        // mybatis-plus // LambdaQueryWrapper<Soft> queryWrapper = new LambdaQueryWrapper<>();
        // mybatis-plex 没有LambdaQueryWrapper， 也不能QueryWrapper<Soft> 这么用！

        // mybatis-flex
        val queryWrapper = QueryWrapper()
        if ("" != softName) {
            // 精确查询
            // queryWrapper.eq(Soft::getSoftName(softName)); // plus
            /*queryWrapper.select(SOFT.ALL_COLUMNS)
                    .where(SOFT.SOFT_NAME.eq(softName));*/

            // 模糊查询
            // queryWrapper.like(Soft::getSoftName(softName)); // plus
            queryWrapper.select(SoftTableDef.SOFT.ALL_COLUMNS) // .orderBy(SOFT.ID) // 报错的 // flex 没有DESC??
                    .where(SoftTableDef.SOFT.SOFT_NAME.like(softName))
        }

        /*QueryWrapper queryWrapper1 = QueryWrapper.create()
                .select(SOFT.ALL_COLUMNS)
                .where(SOFT.SOFT_NAME.eq(softName));*/
        return page(Page(
                current,
                size
        ), queryWrapper)
    }


}